<template>
  <el-form size="mini" label-position='left' label-width="120px">
    <el-form-item label="基本数据">
      <el-tag type="success">分组名称:{{ form.group_name }}</el-tag>
      <el-tag type="success">接口名称:{{ form.api_name }}</el-tag>
    </el-form-item>
    <el-form-item :required="arg.argument_opinion==='required'" :label="arg.key" v-for="(arg, index) in argument"
                  :key="arg.index">
      <el-input :placeholder="arg.description" v-model="arg.value"></el-input>
    </el-form-item>
    <el-form-item required label="任务定时">
      <el-input placeholder="* * * * *" v-model="cron"></el-input>
    </el-form-item>
    <el-form-item required label="任务描述">
      <el-input v-model="description"></el-input>
    </el-form-item>
    <el-form-item label="回调方法">
      <el-input placeholder="回调方法名" style="width: 200px"  v-model="function_name"></el-input>
      <el-input placeholder="回调参数" style="width: 200px" v-model="params"></el-input>
    </el-form-item>
    <br>
    <el-form-item style="float: right">
      <el-button @click="submit" type="primary">保存</el-button>
      <el-button @click="closeTaskForm">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {upsertOneHandler} from "../../assets/js/task";

export default {
  name: "designer-task",
  mounted() {
    this.loadArgument()
  },
  props: {
    form: {},
    taskFormDialogVisible: false,
  },
  data() {
    return {
      argument: [],
      cron: '',
      description: '',
      function_name: '',
      params: '',
    }
  },
  methods: {
    loadArgument: function () {
      this.argument = []
      let argument = this.form.data.argument
      for (let i = 0; i < argument.length; i++) {
        if (argument[i].argument_opinion !== 'static') {
          this.argument.push(argument[i])
        }
      }
    },
    submit: function () {
      let data = {
        api_name: '',
        group_name: '',
        data: {},
        cron: '',
        description: '',
        back_function: {
          function_name: '',
          params: ''
        }
      }
      data.cron = this.cron
      data.description = this.description
      data.api_name = this.form.api_name
      data.group_name = this.form.group_name

      let argument = this.form.data.argument
      for (let i = 0; i < argument.length; i++) {
        if (argument[i].argument_opinion !== 'static') {
          data.data[argument[i].key] = argument[i].value
        }
      }
      upsertOneHandler(data).then(response => {
        if (response.data.code === 0) {
          this.closeTaskForm()
          this.$message({
            message: "提交成功",
            type: "success"
          })
        } else {
          this.$message({
            message: response.data.msg,
            type: 'error'
          })
        }
      })
    },
    closeTaskForm: function () {
      this.$emit("listenToChildEvent", this.taskFormDialogVisible)
    },
  }
}
</script>

<style scoped>

</style>